using JT808Protocol.Common;
using JT808Protocol.Models.BodyParserStrategy.Location.LocationAddition;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy
{
    /// <summary>
    /// 报警附件信息消息
    /// 消息Id：0x1210
    /// 报文类型：信令数据报文
    /// </summary>
    public class DrAlarmAttachment_0x1210 : IJT808Body
    {
        /// <summary>
        /// 终端Id,苏标和粤标不一样
        /// 苏：7个字节，由大写字母和数字组成，此终端ID由制造商自行定义，位数不足时，后补“0x00”
        /// </summary>
        [JsonPropertyName("终端ID")] public string DeviceId { get; set; }

        /// <summary>
        /// 报警标识号
        /// 报警识别号定义见表4-16
        /// </summary>
        [JsonPropertyName("报警标识号")] public AlarmLogo AlarmLogo { get; set; }

        /// <summary>
        /// 报警编号
        /// 平台给报警分配的唯一编号
        /// </summary>
        [JsonPropertyName("报警编号")] public string AlarmNumber { get; set; }

        /// <summary>
        /// 信息类型
        /// 0x00：正常报警文件信息
        /// 0x01：补传报警文件信息
        /// </summary>
        [JsonPropertyName("信息类型")] public byte MessageType { get; set; }

        /// <summary>
        /// 附件数量
        /// 与报警关联的附件数量
        /// </summary>
        [JsonPropertyName("附件数量")] public byte AttachmentCount { get; set; }

        /// <summary>
        /// 附件信息列表
        /// 见表4-24
        /// </summary>
        [JsonPropertyName("附件信息列表")] public List<ItemAlarmAttachment> ItemAlarmAttachmentList { get; set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            ItemAlarmAttachmentList = new List<ItemAlarmAttachment>();

            int AlarmLogoLen = 0;
            if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2019)
            {
                //粤标数据长度为30字节
                DeviceId = ParseHelper.AsciiString(ref source, index, 30).Trim('\0');
                index += 30;
                AlarmLogo = new LaAlarmLogo_Yue();
                AlarmLogoLen = 40;
            }
            else if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2013)
            {
                //苏标数据长度为7字节
                DeviceId = ParseHelper.AsciiString(ref source, index, 7).Trim('\0');
                index += 7;
                AlarmLogo = new LaAlarmLogo_Su();
                AlarmLogoLen = 16;
            }

            //读取报警标识号
            AlarmLogo.ReadEntity(source, ref index);
            index += AlarmLogoLen;

            //读取报警编号
            AlarmNumber = ParseHelper.AsciiString(ref source, index, 32);
            index += 32;

            MessageType = source[index];

            index++;
            AttachmentCount = source[index];

            index++;
            var count = 0;
            while (count < AttachmentCount)
            {
                var length = source[index];
                index++;
                var name = ParseHelper.Gb2312String(ref source, index, length);
                index += length;
                var size = ParseHelper.Uint32(ref source, index);
                index += 4;
                ItemAlarmAttachmentList.Add(new ItemAlarmAttachment()
                {
                    Length = length,
                    Name = name,
                    Size = size
                });
                count++;
            }

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new NotImplementedException();
        }
    }
}